﻿using UnityEngine;
using System.Collections;

public class PlatformVerticalMovement : MonoBehaviour
{
	public float speed;
	public float distance;
	public bool invert;
	
	private Transform PlatformTransform;
	private bool up;
	private float start;
	private float end;

	void Start()
	{
		up = true;
		PlatformTransform = transform;
		start = rigidbody.position.y;
		end = rigidbody.position.y + distance;
		if(invert)
		{
			end = rigidbody.position.y - distance;
		}
	}

	void FixedUpdate()
	{
		if(invert)
		{
			if(up)
			{
				PlatformTransform.position -= Vector3.up * speed;
				
				if(rigidbody.position.y <= end)
				{
					up = false;
				}
			}
			else 
			{
				PlatformTransform.position += Vector3.up * speed;

				if(rigidbody.position.y >= start)
				{
					up = true;
				}
			}
		}
		else
		{
			if(up)
			{
				// going up
				PlatformTransform.position += Vector3.up * speed;
				
				if(rigidbody.position.y >= end)
				{
					up = false;
				}
			}
			else 
			{
				// going down
				PlatformTransform.position -= Vector3.up * speed;
				
				if(rigidbody.position.y <= start)
				{
					up = true;
				}
			}
		}
	}

	void OnControllerColliderHit(ControllerColliderHit hit)
	{
		Debug.Log ("Enter Controller");
	}
	void OnCollisionEnter(Collision other)
	{
		Debug.Log ("Enter");
	}

	void OnCollisionStay(Collision other)
	{
		//other.transform.position.y = transform.position.y + 1f;
		Debug.Log ("Stay");
	}
}